package com.lwj.liwanjia.utils;

import android.os.CountDownTimer;
import android.widget.TextView;

/**
 * 倒计时
 */

public class TimeCountDown1 extends CountDownTimer {
    private TextView tvMinute;
    private TextView tvSecond;

    /**
     * @param millisInFuture    总共的时间戳
     * @param countDownInterval 每次减少的时间，一般为1000
     * @param
     */
    public TimeCountDown1(long millisInFuture, long countDownInterval,TextView tvMinute, TextView tvSecond) {
        super(millisInFuture, countDownInterval);
        this.tvMinute = tvMinute;
        this.tvSecond = tvSecond;
        tvMinute.setClickable(false);
        tvSecond.setClickable(false);
    }

    @Override
    public void onTick(long millisUntilFinished) {
        long l = millisUntilFinished / 1000;
        long minutes = ((l % (24 * 60 * 60)) % (60 * 60)) / 60;
        long second = ((l % (24 * 60 * 60)) % (60 * 60)) % 60;
        String minutesStr = timeStrFormat(String.valueOf(minutes));
        String secondStr = timeStrFormat(String.valueOf(second));
        tvMinute.setText(minutesStr);
        tvSecond.setText(secondStr);
    }

    @Override
    public void onFinish() {
        tvMinute.setText("00");
        tvSecond.setText("00");
    }

    private static String timeStrFormat(String timeStr) {
        switch (timeStr.length()) {
            case 1:
                timeStr = "0" + timeStr;
                break;
        }
        return timeStr;
    }

}
